﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using V5_BLL;
using V5_Common;
using V5_Model;

namespace V5_EShop.Controllers {
    public class ProductController : BaseController {
        // 首页
        public ActionResult Index() {
            return View();
        }


        //列表
        public ActionResult List(int classId, int pageId) {

            string PageUrl = "/product-list-[1]-[2]-[3]-{0}.";
            int count = 0;
            string strWhere = "";

            var _classid = 0;
            var _showid = 0;
            //开始过滤参数
            if (_classid > 0) {
                strWhere += " ClassID=" + _classid;
            }

            DataTable dt = new BLLProduct().GetList(20, pageId, strWhere, ref count).Tables[0];

            this.ViewData["list"] = dt.Rows;
            this.ViewData["pager"] = "";

            return View();
        }

        //搜索
        public ActionResult Search(string classid, int pageid) {
            /*
            string PageUrl = "/Search.aspx?txtsearchkey=" + searchkey
                + "&productclassid=" + _classid
                + "&showid=" + _showid
                + "&orderid=" + _orderid
                + "&low=" + low
                + "&high=" + high
                + "&pageid={0}";*/
            int count = 0;
            int IsDesc = 0;
            string strWhere = " ID>0 ", strOrder = " ID ";

            int _showid = 0;
            var _orderid = 0;
            var searchkey = "";

            //开始过滤参数
            if (0 > 1) {
                strWhere += " and ClassID=" + 0;
            }
            //

            switch (_showid) {
                case 1:
                    //Pager.PageSize = 12;
                    break;
                case 2:
                    //Pager.PageSize = 6;
                    break;
            }
            //销量 价格 排序 默认为1
            switch (_orderid) {
                case 1:
                    strOrder = "ID";
                    IsDesc = 1;
                    break;
                case 2://销售量排序
                    strOrder = "ID";
                    IsDesc = 1;
                    break;
                case 3:
                    strOrder = " convert(int,productmemberprice) ";
                    IsDesc = 0;
                    break;
                case 4:
                    strOrder = " convert(int,productmemberprice) ";
                    IsDesc = 1;
                    break;
            }
            string strOrder2 = strOrder;
            if (IsDesc == 0) {
                strOrder2 += " desc ";
            }
            else {
                strOrder2 += "asc";
            }
            if (searchkey != "" && searchkey.Length > 0) {
                strWhere += "  and ProductName like '%" + searchkey + "%' ";
            }

            DataTable dt = new BLLProduct().GetList(
                20,
                pageid,
                strWhere,
                strOrder,
                IsDesc,
                out count
                ).Tables[0];

            this.ViewData["list"] = dt.Rows;
            this.ViewData["pager"] = "";

            return View();
        }

        //详细
        public ActionResult View(int id) {
            var product = new BLLProduct().GetModel(id, true);
            this.ViewData["view"] = product;
            return View();
        }

        //评论
        public ActionResult Comment(int classId, int pageId) {

            var count = 0;
            DataTable dt = new BLLProductComment().GetList(10,
                1,
                "",
                ref count).Tables[0];

            this.ViewData["list"] = dt.Rows;
            this.ViewData["pager"] = "";

            return View();
        }

        //评论Post
        public ContentResult CommentPost() {

            var pid = InputHelper.SetInt(Request["lhpid"]);
            var Action = InputHelper.SetString(Request["laction"]);
            string s1 = InputHelper.SetString(Request["txtpCommentContent"]);
            string s2 = InputHelper.SetString(Request["txtpCommentEmail"]);
            string s3 = InputHelper.SetString(Request["txtpCommentUserName"]);
            string s4 = InputHelper.SetString(Request["ddlpmost"]);


            BLLProductComment bll = new BLLProductComment();
            ModelProductComment model = new ModelProductComment();
            int ID = bll.GetMaxId();
            string CommentContent = s1;
            string CommentName = s3;
            string CommentEmail = s2;
            string CommentIP = HttpHelper.GetIP;
            string ProductName = new BLLProduct().GetModel(pid, true).ProductName;
            DateTime AddDateTime = DateTime.Now;
            string ReplyComment = "";
            string ReplyName = "";
            int Pmost = InputHelper.SetInt(s4);

            model.ID = ID;
            model.CommentContent = CommentContent;
            model.CommentName = CommentName;
            model.CommentEmail = CommentEmail;
            model.CommentIP = CommentIP;
            model.ProductName = ProductName;
            model.ProductID = pid;
            model.AddDateTime = AddDateTime;
            model.ReplyComment = ReplyComment;
            model.ReplyName = ReplyName;
            model.Pmost = Pmost;

            bll.Add(model);

            return Content("发布成功!");
        }

        //积分兑换
        public ActionResult Integral() {
            return View();
        }

        //热卖
        public ActionResult Hot() {
            return View();
        }

        //问题
        public ActionResult Ask() {
            return View();
        }

        //问题Post
        public ContentResult AskPost() {

            int id = InputHelper.SetInt(Request["Id"]);
            if (InputHelper.SqlFilter(InputHelper.SetString(Request["AskName"])) || InputHelper.SqlFilter(InputHelper.SetString(Request["AskContent"]))) {
                return Content("参数不能为空!");
            }
            string AskName = InputHelper.SetString(Request["AskName"]);
            string AskContent = InputHelper.SetString(Request["AskContent"]);
            if (id > 0) {
                BLLProductAsk bll = new BLLProductAsk();
                ModelProductAsk model = new ModelProductAsk();
                model.AskTitle = AskName;
                model.AskContent = AskContent;
                model.MemberID = 0;
                model.ProductID = id;
                model.Passed = 0;
                model.AddDateTime = DateTime.Now;
                model.GetIP = HttpHelper.GetIP;
                bll.Add(model);
                return Content("您的提问已经提交给管理员审核!请耐心等待");
            }

            return Content("处理成功!");
        }

        //回答
        public ActionResult Answer() {

            var productid = Request["productid"];
            var askid = Request["askid"];

            string strWhere = "";
            int Count = 0;
            strWhere = " productid=" + productid + " and askid=" + askid + " and Passed=1 ";
            DataTable dt = new BLLProductAnswer().GetList(20, 1, strWhere, ref Count).Tables[0];
            this.ViewData["list"] = dt.Rows;
            this.ViewData["pager"] = "";
            return View();
        }

        public ContentResult AnswerPost() {
            int m_productid = InputHelper.SetInt(Request["hfproductid"]);
            int m_askid = InputHelper.SetInt(Request["hfaskid"]);
            string AnswerContent = InputHelper.SetString(Request["answerContent"]);
            if (AnswerContent.Trim() == "") {
                Response.Redirect(HttpHelper.ComeUrl);
            }
            if (m_productid == 0 && m_askid == 0) {
                return Content("对不起!参数有误不能提交!");
            }
            ModelProductAnswer model = new ModelProductAnswer();
            BLLProductAnswer bll = new BLLProductAnswer();
            model.AnswerContent = AnswerContent;
            model.AskID = m_askid;
            model.MemberID = 0;
            model.ProductID = m_productid;
            model.AddDateTime = DateTime.Now;
            model.Passed = 0;
            model.QuestionID = 0;
            model.GetIP = HttpHelper.GetIP;
            bll.Add(model);
            return Content("恭喜您!您的回答已经成功!请等待管理员的审核!");
        }


        #region
        //绑定多图
        private void Bind_ProductMorePic(ModelProduct product) {
            DataTable dt = new BLLProduct().Get_ProductMorePics(product.ProductMorePic);
        }

        //绑定可选属性
        private void Bind_CustomChoice(ModelProduct product) {
            DataTable customChoice = product.Choicetable;
            if (customChoice != null) {
                DataView dv = customChoice.DefaultView;
            }
        }

        //获取客户端
        protected string Get_CustomChoice(int indexid, int filedid, string FieldValue) {
            ModelProductField modelproductfield = new BLLProductField().GetModel(filedid);
            if (modelproductfield.IsUseImg == 1)//判断是否用户可以选择
            {
                string fieldName = modelproductfield.FieldName;
                string fieldvalues = modelproductfield.FieldValues;
                string fieldpics = modelproductfield.FieldPics;

                string[] fieldvalueArr = fieldvalues.Split(new char[] { ',' });
                string[] filedpicArr = fieldpics.Split(new char[] { ',' });

                string[] s1 = FieldValue.Split(new char[] { ',' });

                StringBuilder sb = new StringBuilder();
                sb.Append("<li>");
                sb.Append(@"<span id=""C_custom_" + indexid + @""" class=""hong""><span class=""hui"">" + fieldName + @"：</span><span >" + s1[0] + "</span> </span>");
                sb.Append(@"<span class=""mianliao"">");


                for (int n = 0; n < s1.Length; n++) {
                    for (int i = 0; i < fieldvalueArr.Length; i++) {
                        for (int j = 0; j < filedpicArr.Length; j++) {
                            if (i == j && s1[n] == fieldvalueArr[i]) {
                                sb.Append(@"<img src=""UpLoadFiles/productFiled/" + filedpicArr[j] + @""" onclick=""changeCustom('custom_" + indexid + @"','" + fieldName + @"：" + s1[n] + @"')"">");
                            }
                        }
                    }
                }
                sb.Append(@"</span>");
                sb.Append(@"<div class=""clear""></div></li>");
                return sb.ToString();
            }
            return "";
        }
        //绑定其他字段属性
        protected void Bind_ProductPro(ModelProduct product) {
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("m1"));
            dt.Columns.Add(new DataColumn("m2"));
            if (product.ProductSingleFields.Trim().Length > 0) {
                string[] sarr1 = product.ProductSingleFields.Split(new string[] { "||" }, StringSplitOptions.None);
                foreach (string s1 in sarr1) {
                    string[] sarr2 = s1.Split(new string[] { ":" }, StringSplitOptions.None);
                    DataRow dr = dt.NewRow();
                    dr["m1"] = sarr2[0];
                    dr["m2"] = sarr2[1];
                    dt.Rows.Add(dr);
                }
            }
        }
        public int CommentCount = 0;
        private void Bind_ProductComment(ModelProduct product) {

            string strWhere = "";
            strWhere = " ProductID=" + product.ID + " and Passed=1 ";

            DataTable dt = new BLLProductComment().GetList(0, strWhere, " ID Desc").Tables[0];
            CommentCount = dt.Rows.Count;
        }

        protected string Get_SysAnswer(string replyComment) {
            if (replyComment != "" && replyComment.Length > 0) {
                return @"<div class=""answers"">
                                    <strong>管理员 回答：</strong> <span style=""display: block; margin: 10px 0 0 0px; text-indent: 24px;"">" + replyComment + @"</span>
                                </div>";
            }
            return "";
        }

        /// <summary>
        /// 绑定洗涤
        /// </summary>
        /// <param name="xdstrings"></param>
        /// <returns></returns>
        public string Get_ProductXJ(string xdstrings, int _width, int _height) {
            string tempstr = "";
            if (xdstrings != "" && xdstrings.Length > 0) {
                foreach (string s1 in xdstrings.Split(new string[] { "," }, StringSplitOptions.None)) {
                    tempstr += "<img src='/images/xd/xd_" + s1 + ".jpg' width='" + _width + "' height='" + _height + "' alt='洗涤'/>";
                }
            }
            return tempstr;
        }

        #region 绑定问答
        public int QuestionCount = 0;
        //绑定提问
        private void Bind_ProductQuestionAndAsk(ModelProduct product) {
            string strWhere = "";
            strWhere = " ProductID=" + product.ID + " and Passed=1 ";
            DataTable dt = new BLLProductAsk().GetList(2, strWhere, " ID Desc ").Tables[0];
            QuestionCount = dt.Rows.Count;
            ask_answer_count = dt.Rows.Count;
        }
        /// <summary>
        /// 绑定回答
        /// </summary>
        /// <param name="questionid"></param>
        /// <returns></returns>
        public DataTable Bind_ProductAsk(object questionid) {
            return new BLLProductAnswer().GetList(5, " ID=" + questionid.ToString() + " and passed=1 ", " ID desc ").Tables[0];
        }
        public int ask_answer_count = 0;

        #endregion

        /// <summary>
        /// 绑定导航
        /// </summary>
        /// <param name="productID"></param>
        /// <returns></returns>
        public string BindClass(int classID) {
            ModelProductClass model1 = new BLLProductClass().GetModel(classID);
            int parentID1 = model1.ParentID;
            ModelProductClass model2 = null;
            return "";
        }
        #endregion
    }
}